package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;
import com.mobilesales.android.util.CustomFilter;
import com.mobilesales.android.util.DateUtil;

public class MessageAdapter extends ArrayAdapter<Message> {

	private Map<Integer, String> employeeNames;
	private List<Message> items = null;

	public MessageAdapter(Context context, int textViewResourceId,
			List<Message> objects, List<Employee> employees) {
		super(context, textViewResourceId, objects);
		items = new ArrayList<Message>(objects);
		employeeNames = new HashMap<Integer, String>();
		if (employees != null) {
			for (Employee e : employees) {
				employeeNames.put(e.Id, e.Lastname + " " + e.Firstname);
			}
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.message_list_item, null);
		}

		Message msg = null;
		if (position >= 0 && position < this.getCount()) {
			msg = this.getItem(position);
		}

		if (msg != null) {
			TextView contentField = (TextView) v.findViewById(R.id.msgSubject);
			TextView senderField = (TextView) v.findViewById(R.id.msgSenderTxt);
			TextView dateField = (TextView) v.findViewById(R.id.msgDateTxt);
			ImageView icon = (ImageView) v
					.findViewById(R.id.message_adapter_icon);
			contentField.setText(msg.Subject);
			String senderName = this.employeeNames.get(msg.SenderId);
			if (senderName == null)
				senderName = msg.SenderLogin;
			String from = String.format("%s: %s",
					this.getContext().getString(R.string.From), senderName);
			senderField.setText(from);

			dateField.setText(DateUtil.toMessageDateString(msg.SendDate));

			if (msg.IsNew) {
				contentField.setTypeface(null, Typeface.BOLD);
				contentField.setTextColor(v.getResources()
						.getColor(R.color.red));
				icon.setImageResource(R.drawable.mail_new);
				senderField.setTypeface(null, Typeface.BOLD);
			} else {
				contentField.setTypeface(null, Typeface.NORMAL);
				contentField.setTextColor(v.getResources().getColor(
						R.color.black));
				icon.setImageResource(R.drawable.mail);
				senderField.setTypeface(null, Typeface.NORMAL);
			}
		}

		return v;
	}

	@Override
	public Filter getFilter() {
		return new CustomFilter<Message>(this, items) {

			@Override
			protected boolean fits(String query, Message m) {
				String senderIdentity = MessageAdapter.this.employeeNames
						.get(m.SenderId);
				if (senderIdentity != null)
					if (senderIdentity.toLowerCase().contains(
							query.toLowerCase()))
						return true;

				if (m.Subject.toLowerCase().contains(query.toLowerCase()))
					return true;

				return false;

			}
		};
	}
}
